package com.itheima.admin.service.impl;

import com.alibaba.nacos.common.utils.CollectionUtils;
import com.itheima.admin.mapper.AdChannelMapper;
import com.itheima.admin.pojo.AdChannel;
import com.itheima.admin.service.XxlJobService;
import com.itheima.article.dto.ArticleInfoDto;
import com.itheima.article.fagin.AddArticleInfoDtoFeign;
import com.itheima.article.pojo.ApArticle;
import com.itheima.article.pojo.ApArticleConfig;
import com.itheima.article.pojo.ApArticleContent;
import com.itheima.common.exception.LeadNewsException;
import com.itheima.common.vo.ResultVo;
import com.itheima.wemedia.dto.WmNewsDtoSave;
import com.itheima.wemedia.fagin.AdWmNewsFeign;
import com.itheima.wemedia.vo.WmNewsVo;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.invoke.LambdaConversionException;
import java.util.List;

@Service
@Slf4j
public class XxlJobServiceImpl implements XxlJobService {

    //注入频道mapper层的接口
    @Autowired
    private AdChannelMapper adChannelMapper;

    @Autowired
    private AddArticleInfoDtoFeign addArticleInfoDtoFeign;
    /**
     * 远程调用文章同步
     * @param wmNewsVo 接收文章调度参数
     * @return 返回是否同步成功
     */
    @Override
    public ResultVo syncArticleJob(WmNewsVo wmNewsVo) {
        //创建一个ArticleInfoDto对象
        ArticleInfoDto articleInfoDto = new ArticleInfoDto();
        //封装发布文章文章
        ApArticle apArticle = setApArticle(articleInfoDto,wmNewsVo);
        //填入apArticleConfig对象所需数据
        ApArticleConfig apArticleConfig = getApArticleConfig();
        ApArticleContent apArticleContent = getApArticleContent(wmNewsVo);
        //设置到articleInfoDto中
        articleInfoDto.setApArticle(apArticle);
        articleInfoDto.setApArticleConfig(apArticleConfig);
        articleInfoDto.setApArticleContent(apArticleContent);
        articleInfoDto.setApUserId(wmNewsVo.getApUserId());
        articleInfoDto.setWmNewsId(wmNewsVo.getId());
        articleInfoDto.setWmUserId(wmNewsVo.getUserId());
        //调用文章库微服务进行添加操作
        ResultVo resultVo = addArticleInfoDtoFeign.addArticleInfoDtoFeign(articleInfoDto);
        //判断是否添加成功
        if (!resultVo.isSuccess()){
            throw new LeadNewsException("添加文章失败");
        }
        return ResultVo.ok();
    }

    /**
     * 同步文章内容赋值
     * @param wmNewsVo
     * @return
     */
    private ApArticleContent getApArticleContent(WmNewsVo wmNewsVo) {
        ApArticleContent apArticleContent = new ApArticleContent();
        apArticleContent.setContent(wmNewsVo.getContent());
        return apArticleContent;
    }
    /**
     * 同步文章配置赋值
     * @return
     */
    private ApArticleConfig getApArticleConfig() {
        ApArticleConfig apArticleConfig = new ApArticleConfig();
        apArticleConfig.setIsComment(1);//是否可评论初始化默认可评论1
        apArticleConfig.setIsDelete(0);//是否已删除，初始话默认为0，未删除
        apArticleConfig.setIsDown(0);//是否可以下架，默认是上架状态
        apArticleConfig.setIsForward(1);//是否可以转发，默认是1，可以转发
        return apArticleConfig;
    }

    /**
     * 同步文章赋值
     * @param articleInfoDto
     * @param wmNewsVo
     * @return
     */
    private ApArticle setApArticle(ArticleInfoDto articleInfoDto,WmNewsVo wmNewsVo) {
        ApArticle apArticle = new ApArticle();
        BeanUtils.copyProperties(wmNewsVo,apArticle);
        //补全没有拷贝上的数据
        apArticle.setAuthorId(wmNewsVo.getApUserId());
        //根据文章id查询文章名称
        AdChannel adChannel = adChannelMapper.selectById(wmNewsVo.getChannelId());
        //设置文章id，后面添加文章id判断是否是，添加文章，还是修改文章
        apArticle.setId(wmNewsVo.getArticleId());
        apArticle.setChannelName(adChannel.getName());
        apArticle.setFlag(0);
        apArticle.setLikes(0);
        apArticle.setCollection(0);
        apArticle.setComment(0);
        apArticle.setViews(0);
        apArticle.setSyncStatus(0);//是否同步到es，0表示未同步到es
        apArticle.setOrigin(1);//来源1表示自媒体，2表示是爬虫
        apArticle.setLayout(wmNewsVo.getType());
        return apArticle;
    }
}
